<!DOCTYPE html>
<html>
<head>
  <title>Energia Reference - Tutorials </title>
  <link rel="shortcut icon" type="image/x-icon" href="http://energia.nu/img/eicon.png">
  <link rel='stylesheet' href='energiaWideRender.css' 
type='text/css' />
    <!--HeaderText--><style type='text/css'></style>  
  <meta name='robots' content='index,follow' />
  <meta name="Author" content="Robert Wessels" />
  <meta name="Publisher" content="Energia" />
  <meta name="Keywords" content="Energia, Wiring, Processing, Maker, LaunchPad, Arduino, Texas Instruments, Sketch, MSP430, mspgcc, mpsdebug, Electronic Arts, Programming, C, C++, Robert Wessels" />
  <meta name="Description" content="Energia is a rapid prototyping platform for the Texas Instruments MCU Launchpad. Energia is based on Wiring and Arduino and uses the Processing IDE." />
  <meta name="Copyright" content="All contents copyright Robert Wessels" />

</head>
<body>
<div id="page">
  <!--PageHeaderFmt-->
  <div id="pageheader">
    <div class="title"><a href='http://energia.nu'>Energia</a></div>
  </div>
  <!--/PageHeaderFmt-->

  <!--PageLeftFmt-->
  <div id="pagenav">
    <div id="navbar">
  	<p><a class='wikilink' href='http://energia.nu/'>Home</a>
<a class='wikilink' href='http://energia.nu/download'>Download</a>
<a class='wikilink' href='Guide_index.html'>Getting Started</a>
<a class='wikilink' href='index.html'>Reference</a>
<a class="wikilink" href="http://energia.nu/help.html">Getting Help</a>
<a class="nav" href="http://energia.nu/faq.html">FAQ</a>
<a class="wikilink" href="http://energia.nu/projects.html">Projects Using Energia</a>
<a class="nav" href="http://energia.nu/contact.html">Contact Us</a>
</p>


<p class='vspace'></p>

    </div>
  </div>
  <!--/PageLeftFmt-->

  <div id="pagetext">
  <!--PageText-->
<div id='wikitext'>
<p><em>Examples &gt; Strings</em>
</p>
<p class='vspace'></p><h3>String Constructors</h3>
<p>This example will go over how to use string constructors.
</p>
<h3>Relevant Groundwork</h3>
<p>The String object allows you to manipulate strings of text in a variety of useful ways. You can append characters to Strings, combine Strings through concatenation, get the length of a String, search and replace substrings, and more. This tutorial shows you how to initialize String objects.
</p><p class='vspace'></p>
<pre>
 String stringOne = "Hello String";                   // using a constant String
 String stringOne =  String('a');                     // converting a constant char into a String
 String stringTwo =  String("This is a string");      // converting a constant string into a String object
 String stringOne =  String(stringTwo + " with more");// concatenating two strings
 String stringOne =  String(13);                      // using a constant integer
 String stringOne =  String(analogRead(0), DEC);      // using an int and a base
 String stringOne =  String(45, HEX);                 // using an int and a base (hexadecimal)
 String stringOne =  String(255, BIN);                // using an int and a base (binary)
 String stringOne =  String(millis(), DEC);           // using a long and a base
</pre><p class='vspace'></p>
<p>All of these methods are valid ways to declare a String object. They all result in an object containing a string of characters that can be manipulated using any of the String methods. To see them in action, upload the code below onto an Arduino and open the Serial Monitor. You'll see the results of each declaration. Compare what's printed by each println() to the declaration above it.
</p>
<h3>Hardware Required</h3>
<ul>
<li>MSP-EXP430G2 LaunchPad</li>
</ul>
<h3>Circuit</h3>
<p>Only LaunchPad is required for this example.
</p>
<img height='250px' src='../reference/img/Blink_bb.png' alt='' title='' />
<p>image developed using <a href='http://fritzing.org'>Fritzing</a>. For more circuit examples, see the <a href='http://fritzing.org/projects'>Fritzing project page</a>.
</p>
<h3>Schematic</h3>
<img height='400px' src='../reference/img/Blink_schem.png' alt='' title='' />
<h3>Code Explanation</h3>
<p>In the program, we use string contructors to create strings from other data types.
</p>
<h3>Code</h3>
<pre>
/*
   String constructors
 
 Examples of how  to create strings from other data types
 
 created 27 July 2010
 modified 30 Aug 2011
 by Tom Igoe
 
 This example code is in the public domain.
 */

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  
  // send an intro:
  Serial.println("\n\nString Constructors:");
  Serial.println();
}

void loop() {
  // using a constant String:
  String stringOne = "Hello String";     
  Serial.println(stringOne);      // prints "Hello String"

  // converting a constant char into a String:
  stringOne =  String('a');        
  Serial.println(stringOne);       // prints "a"

  // converting a constant string into a String object:
  String stringTwo =  String("This is a string");  
  Serial.println(stringTwo);      // prints "This is a string"

  // concatenating two strings:
  stringOne =  String(stringTwo + " with more"); 
  // prints "This is a string with more":
  Serial.println(stringOne);      

  // using a constant integer:
  stringOne =  String(13);        
  Serial.println(stringOne);      // prints "13"

  // using an int and a base:
  stringOne =  String(analogRead(A0), DEC);  
  // prints "453" or whatever the value of analogRead(A0) is
  Serial.println(stringOne);      

  // using an int and a base (hexadecimal):
  stringOne =  String(45, HEX);   
  // prints "2d", which is the hexadecimal version of decimal 45:
  Serial.println(stringOne);      

  // using an int and a base (binary)
  stringOne =  String(255, BIN);  
  // prints "11111111" which is the binary value of 255
  Serial.println(stringOne);      

  // using a long and a base:
  stringOne =  String(millis(), DEC);
  // prints "123456" or whatever the value of millis() is: 
  Serial.println(stringOne);      

  // do nothing while true:
  while(true);

}
</pre>
<h3>Working Video</h3>
(Insert Video Here)
<h3>Try it out:</h3>
<p>- Use all the possible string constructors in a sketch
</p>
<p class='vspace'></p><p><strong>See Also</strong>
</p><ul><li><a class='wikilink' href='Setup.html'>setup</a>()
</li><li><a class='wikilink' href='Loop.html'>loop</a>()
</li><li><a class='wikilink' href='Serial_Println.html'>println</a>()
</li></ul>

<p class='vspace'></p><p><a class='wikilink' href='Guide_index.html'>Getting Started Home</a>
</p>
<p class='vspace'></p><p><em>Corrections, suggestions, and new documentation should be posted to the <a class='urllink' href='http://forum.43oh.com/forum/28-energia/' rel='nofollow'>Forum</a>.</em>
</p>
<p class='vspace'></p><p>The text of the Energia reference is licensed under a
<a class='urllink' href='http://creativecommons.org/licenses/by-sa/3.0/' rel='nofollow'>Creative Commons Attribution-ShareAlike 3.0 License</a>.  Energia reference is based on the Arduino reference. Code samples in the reference are released into the public domain.
</p>
</div>


  </div>

  <!--PageFooterFmt-->
  <div id="pagefooter">
&copy;Energia | 
  <a href='#'>Edit Page</a> | <a href='#'>Page History</a> | <a href=#' target='_blank'>Printable View</a> | <a href='#'>All Recent Site Changes</a>
  </div>
  <!--/PageFooterFmt-->

</div>
</body>
</html>
